<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
	private $_id;
	public $user;
	
	public function authenticate()
	{
		$this->user=User::model()->findByAttributes(array('email'=>$this->username));
		if($this->user===null)
			$this->errorCode=self::ERROR_USERNAME_INVALID;
		else if($this->user->pwd!==$this->password)
			$this->errorCode=self::ERROR_PASSWORD_INVALID;
		else {
			$this->_id = $this->user->id;
			
			$this->setState('id'		,$this->user->id);
			$this->setState('email'		,$this->user->email);
			$this->setState('name'		,$this->user->name);
			$this->setState('pwd'		,$this->user->pwd);
			$this->setState('group'		,$this->user->user_group_id);
			
			switch ($this->user->user_group_id) {
				case '1':
					$role = 'admin';
					break;
				case '2':
					$role = 'user';
					break;
				default:
					$role = 'guest';
					break;
			}
			
			$this->setState('role',$role);
			
			$this->errorCode = self::ERROR_NONE;
		}
		return !$this->errorCode;
	}
	
	public function status() {
		if ($this->user->user_status_id==1) {
			return true;
		}
		return false;
	}
	
	public function getId() {
		return $this->_id;
	}
}